
package bean;

import dao.ClienteFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;
import model.Cliente;


@ManagedBean
@SessionScoped
public class ClienteBean {
    @EJB
    private ClienteFacade clienteFacade;

    
    private Cliente selected;
   
    public ClienteBean() {
    }
    
    private List<Cliente> listado(){
        return clienteFacade.findAll();
    }

    public Cliente getSelected() {
         if(selected == null){
            selected = new Cliente();
        }
        return selected;
    }

    public void setSelected(Cliente selected) {
        this.selected = selected;
    }
    
    
     // --------------------- Métodos de Ayuda para acceder al Bean por otras Clases ---------------------

    public SelectItem[] getItemsAvailableSelectOne() {
        return getSelectItems(clienteFacade.findAll(), true);
    }

    // Genera una lista con los items seleccionados (uno o muchos según selectOne). Para tablas relacionadas.
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {

        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "Selecciona");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;

    }
    
    // ------------------ Clase para conversiones, se deben implementar todos los métodos ------------------
    @FacesConverter(forClass = Cliente.class)
    public static class ClienteControllerConverter implements Converter {

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        } 

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        } 

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) { return null; }
            ClienteBean controller = (ClienteBean) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "clienteBean");
            return controller.clienteFacade.find(getKey(value));
        } 

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) { return null; }
            if (object instanceof Cliente) {
                Cliente o = (Cliente) object;
                return getStringKey(o.getIdCliente());
            }
            else { throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ClienteBean.class.getName()); }
        } 

    } 
    
    
    
}
